package com.java01.misc.eventdriven;

import java.util.ArrayList;
import java.util.List;

/**
 * 4 Custom singleton event listener
 * Creates a simple singleton event listener for CustomEvent. Can be invoked anywhere in your application. 
 */
public class CustomEventSingletonFirer {
	private List<CustomEventListener> listeners = new ArrayList<CustomEventListener>();
	private static CustomEventSingletonFirer self;
	
	private CustomEventSingletonFirer(){
	}
	
	public static CustomEventSingletonFirer getInstance(){
		if(self==null){
			self = new CustomEventSingletonFirer();
		}
		return self;
	}
	
	public void addCustomEventListener(CustomEventListener listener){
		if(!listeners.contains(listener)){
			listeners.add(listener);
		}
	}
	
	public void removeCustomEventListener(CustomEventListener listener){
		if(listeners.contains(listener)){
			listeners.remove(listener);
		}
	}
	
	public void fireCustomEvent(CustomEvent event){
		for(CustomEventListener listener:listeners){
			listener.customEventOccurs(event);
		}
	}
}
